package com.fadis.cmd;

import com.fadis.FadisException;
import com.fadis.data.zset.ZSet;
import com.fadis.data.zset.ZSkipListNode;
import com.fadis.db.FadisDB;
import com.fadis.protocol.BulkString;
import com.fadis.protocol.RespArray;

import java.util.ArrayList;
import java.util.List;

class CmdUtils {

    public static class ZRangeByScoreParams{
        public ZSetScoreRange range;

        public boolean limit;
        public long offset;
        public long count;
        public boolean withScores;
        public boolean reverse;

        @Override
        public String toString() {
            return "ZRangeByScoreParams{" +
                    "range=" + range +
                    ", limit=" + limit +
                    ", offset=" + offset +
                    ", count=" + count +
                    ", withScores=" + withScores +
                    ", reverse=" + reverse +
                    '}';
        }

        public static ZRangeByScoreParams from(FadisActionFrame frame, boolean reverse){
            ZRangeByScoreParams p = new ZRangeByScoreParams();
            p.reverse = reverse;
            String minStr = frame.getArg(reverse ? 3 : 2);
            String maxStr = frame.getArg(reverse ? 2 : 3);
            p.range = ZSetScoreRange.from(minStr, maxStr);

            int currentIndex = 4;
            String withScoresStr = frame.getArg(currentIndex);
            p.withScores = "withscores".equalsIgnoreCase(withScoresStr);
            if(p.withScores){
                currentIndex += 1;
            }

            String limitStr = frame.getArg(currentIndex);
            p.limit = "limit".equalsIgnoreCase(limitStr);
            if(p.limit){
                currentIndex += 1;
            }

            if(p.limit){
                String offsetStr = frame.getArg(currentIndex);
                String countStr = frame.getArg(currentIndex + 1);
                p.offset = Long.parseLong(offsetStr);
                p.count = Long.parseLong(countStr);
            }else{
                p.offset = 0;
                p.count = Long.MAX_VALUE;
            }
            return p;
        }
    }


}
